﻿using Application.ProductService;
using Application.Purchase_dService;
using AutoMapper;
using Domain.Entity;
using Domain.Entity.bs;
using Domain.Entity.ord;
using Domain.Entity.search;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.Purchase_hService
{
    public class Purchase_hService : IPurchase_hService
    {
        private readonly IPurchase_hRepository purchase_HRepository;
        private readonly IMapper mapper;

        public Purchase_hService(IPurchase_hRepository purchase_HRepository,IMapper mapper)
        {
            this.purchase_HRepository = purchase_HRepository;
            this.mapper = mapper;
        }

        //新增进货单
        public async Task<int> AddPurchase_h(AddPurchase_hDto addPurchase_HDto)
        {
            //转换
            Purchase_h purchase_H = mapper.Map<Purchase_h>(addPurchase_HDto);
            return await purchase_HRepository.AddPurchase_h(purchase_H);
        }


        //审核进货单
        public async Task<int> ApproverPurchase_h(int purid, int status, string approvername, string approvertime)
        {
            return await purchase_HRepository.ApproverPurchase_h(purid, status, approvername, approvertime);
        }


        //删除进货单
        public async Task<int> DeletePurchase_h(int id)
        {
            return await purchase_HRepository.DeletePurchase_h(id);
        }


        //高级查询
        public async Task<FenYe<Purchase_h>> GetAdvancedPurchase_h(FenYe<Purchase_h> fenYe, string? code, string? starttotal, string? endtotal, string? startdate, string? enddate, string? startnum, string? endnum, int? status, int? sup, string? suplink, int? dep, int? user, string? createname, string? startcreatetime, string? endcreatetime, string? approvername, string? startapprovertime, string? endapprovertime)
        {
            return await purchase_HRepository.GetAdvancedPurchase_h(fenYe, code, starttotal, endtotal, startdate, enddate, startnum, endnum, status, sup, suplink, dep, user, createname, startcreatetime, endcreatetime, approvername, startapprovertime, endapprovertime);
        }


        //查询所有进货单据
        public async Task<List<Purchase_h>> GetAllPurchase_h()
        {
            return await purchase_HRepository.GetAllPurchase_h();
        }


        //根据货品id查询进货记录
        public async Task<List<Purchase_h>> GetAllPurchase_hBypid(int procid)
        {
            return await purchase_HRepository.GetAllPurchase_hBypid(procid);
        }


        //根据进货编号查询主键id和进货总数
        public async Task<Purchase_h> Getid(string code)
        {
            return await purchase_HRepository.Getid(code);
        }


        //分页查询进货单
        public async Task<FenYe<Purchase_h>> GetPurchase_h(FenYe<Purchase_h> fenYe, string? code, int? status, int? sup, string? starttime, string? endtime)
        {
            return await purchase_HRepository.GetPurchase_h(fenYe,code, status, sup, starttime, endtime);
        }
        
        //根据id查询进货单
        public async Task<Purchase_h> GetPurchase_HById(int id)
        {
            return await purchase_HRepository.GetPurchase_HById(id);
        }


        //进货统计
        public async Task<PurchaseTJResult> Purchase_hTJ(string? timeType, string? code, int? sup, int? user, int? dep, string? starttime, string? endtime, int? page, int? pagesize)
        {
            return await purchase_HRepository.Purchase_hTJ(timeType,code,sup,user,dep,starttime,endtime,page,pagesize);
        }


        //更新入库数量和单据状态
        public async Task<int> Updatenum(UpdatehNum updateNum)
        {
            Purchase_h purchase_H = mapper.Map<Purchase_h>(updateNum);
            return await purchase_HRepository.Updatenum(purchase_H);
        }

        //修改进货单
        public async Task<int> UpdatePurchase_h(UpdatePurchase_hDto updatePurchase_HDto)
        {
            //转换
            Purchase_h purchase_H = mapper.Map<Purchase_h>(updatePurchase_HDto);
            return await purchase_HRepository.UpdatePurchase_h(purchase_H);
        }


        //更新已退货数量
        public async Task<int> UpdateRNum(int purid, int returnnum, int status)
        {
            return await purchase_HRepository.UpdateRNum(purid, returnnum, status);
        }



        public async Task<int> UpdateRNum(int purid, int returnnum, int status, int newnum)
        {
            return await purchase_HRepository.UpdateRNum(purid,returnnum,status,newnum);
        }


        //修改进货单据状态
        public async Task<int> UpdateStatus(int purid, int status)
        {
            return await purchase_HRepository.UpdateStatus(purid, status);
        }
    }
}
